/*
 * Copyright (C) 2012 Google Inc.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Creates the embed element and calls the one and only function on the plugin
 *   <plugin element>.gtvremote.plugin.object.GTVRemote();
 * That returns an googletvremote object which has 6 functions:
 *   init(localAddress)
 *   loadCert(pemKeyCertificate, privateKeyPassword)
 *   generateSelfSignedCert(privateKeyPassword)
 *   createDiscoveryClient()
 *   createPairingSession()
 *   createAnymoteSession()
 * Using the created objects is handled by an associated controller.
 *   discovery_client_controller.js
 *   pairing_session_controller.js
 *   anymote_session_controller.js
 */


/**
 * Creates the plugin embed, calls the one plugin method to create the
 * googletvremote. Initialize and load the 512 byte certificate, then
 * creates the three controls used for communication.
 *   Discovery client - Used to find Google TV's on the local network.
 *   Pairing session - Used to securely pair with a Google TV.
 *   Anymote session - Used to communicate with a paired Google TV.
 */
var googletvremoteInitializePlugin = function() {
  // DONE: Create an embed of type application/x-gtvremote
  // If the plugin is already present remove it and start fresh.
  var pluginEl = document.getElementById('pluginId');
  if (pluginEl) {
    pluginEl.parentNode.removeChild(pluginEl);
    document.body.className = '';
  }
  pluginEl = document.createElement('embed');
  pluginEl.type = 'application/x-gtvremote';
  pluginEl.id = 'pluginId';
  pluginEl.style.marginLeft = '120px';  // Center just for this demo.
  // You could choose to hide the embed object if you wanted as well.
  //pluginEl.style.position = 'absolute';
  //pluginEl.style.visibility = 'hidden';
  document.body.appendChild(pluginEl);
  document.getElementById('initialize-plugin').innerHTML =
    'Delete old plugin and make a new one';

  // DONE: Call a function on that embed .gtvremote.plugin.object.GTVRemote();
  // This gives you the googletvremote object so you can use JavaScript methods.
  var googletvremote = pluginEl.gtvremote.plugin.object.GTVRemote();

  // DONE: Initialize the googletvremote object.
  googletvremote.init(''); // Sending an empty string is recommended and will
      // let the plugin determine the local ip address of this machine.

  // An Anymote client is identified by its 512 byte certificate and IP address.
  // DONE: Load a certificate into the googletvremote object
  // Save the certificate to localStorage and restore it if available.
  var cert = localStorage.getItem('cert');
  if (!cert || !googletvremote.loadCert(cert, 'password')) {
    // Recommended you change the 'password' to a unique string for your app.
    console.log('First launch. Had to make a new certificate.');
    var uid = (((1 + Math.random()) * 0x10000000) | 0).toString(16);
    cert = googletvremote.generateSelfSignedCert('chrome-gtv-' + uid,
        'password');
    localStorage.setItem('cert', cert);
    localStorage.removeItem(STORAGE_KEY_PAIRED_DEVICES);
  }

  // DONE: Create the three clients used by the plugin.
  discoveryClient = googletvremote.createDiscoveryClient();
  pairingSession = googletvremote.createPairingSession();
  anymoteSession = googletvremote.createAnymoteSession();
};
